package com.mall.biz.trade.impl.o2o.incluce;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.biz.trade.O2OTradeBiz;
import com.mall.client.user.TransferAuditService;
import com.mall.client.user.UserExtendedService;
import com.mall.client.user.UserLevelService;
import com.mall.client.user.UserService;
import com.mall.dto.TradePriceAndBuyCountDTO;
import com.mall.pojo.Trade;
import com.mall.pojo.TransferAudit;
import com.mall.pojo.User;
import com.mall.pojo.UserExtended;
import com.mall.pojo.UserLevel;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.MoneyUtil;
import com.mall.web.util.WebUtil;
@Service
public class YueRechargeTradeImpl implements O2OTradeBiz {
	private static final Logger log = LoggerFactory.getLogger(YueRechargeTradeImpl.class);
	@Autowired
	private UserService userService;
	@Autowired
	private TransferAuditService transferAuditService;
	@Autowired
	private UserExtendedService userExtendedService;
	@Autowired
	private UserLevelService userLevelService;
	

	@Override
	public TradePriceAndBuyCountDTO pack4PirceAndBuyCount(HttpServletRequest request, Integer type, Long price,
			Integer buyCount, Long parentId) {
		TradePriceAndBuyCountDTO tradePriceAndBuyCountDTO = new TradePriceAndBuyCountDTO();
		tradePriceAndBuyCountDTO.setPrice(price);
		tradePriceAndBuyCountDTO.setBuyCount(1);
		return tradePriceAndBuyCountDTO;
	}

	@Override
	public Boolean afterSuccInsert(HttpServletRequest request, Trade trade) {
		Boolean succ = false;
		Long transferAuditId = HttpUtil.getLongValueFromRequest(request, "transferAuditId");
		Integer status = HttpUtil.getIntegerValueFromRequest(request, "status");
		TransferAudit transferAudit = this.transferAuditService.getTransferAuditById(transferAuditId);
		if(transferAudit == null || status == null){
			log.error("transferAudit不存在");
			return succ;
		}
		Long bond = MoneyUtil.getPriceByString(HttpUtil.getStringValueFromRequest(request, "bond"));
		Long balance = MoneyUtil.getPriceByString(HttpUtil.getStringValueFromRequest(request, "balance"));
		User user = this.userService.getUserByUserId(transferAudit.getUserId());
		UserExtended userExtended = this.userExtendedService.getUserExtendByUserId(transferAudit.getUserId());
		if(userExtended == null || user == null){
			return succ;
		}
		userExtended.setBalance(userExtended.getBalance()+balance);
		userExtended.setBond(userExtended.getBond()+bond);
		this.userExtendedService.updateExtendedByPrimaryKey(userExtended);
		
		//如果用户还不是分销商，充值完成后完成升级
		User fenUser = this.userService.getUserByUserId(transferAudit.getUserId());
		if(fenUser == null){
			return succ;
		}
		if(fenUser.getLevel() != null && fenUser.getLevel() < 0){
			Short wantLevel = Short.valueOf(fenUser.getLevel().toString().replace("-", ""));
			UserLevel userlevel = this.userLevelService.getUserLevelByLevelAndKey(wantLevel.intValue(), fenUser.getCommOnlyKey());
			if(userlevel == null){
				return succ;
			}
			if(userExtended.getBond() >=  userlevel.getLowPrice() && userExtended.getBalance() >= userlevel.getHighPrice()){
				user.setLevel(wantLevel);
				this.userService.modifytUserByPrimaryKey(user);
			}
		}
		
		//更新审核状态
		User currentUser = WebUtil.getCurrentUser(request);
		transferAudit.setOperationUserId(currentUser.getId());
		transferAudit.setStatus(status);
		this.transferAuditService.modifyTransferAudit(transferAudit);
		return true;
	}

	@Override
	public Boolean afterErrorInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean beforeInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

}
